//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.yuebanmaoshare.file.common.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class JjwtUtil {
    public static final String JWT_ID = UUID.randomUUID().toString();
    public static final String JWT_SECRET = "jiamimiwen";
    public static final int EXPIRE_TIME = 604800000;

    public static SecretKey generalKey() {
        String secret = "jiamimiwen";
        byte[] encodedKey = Base64.decodeBase64("jiamimiwen");
        SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
        return key;
    }

    public static String createJWT(String issuer, String audience, String subject) throws Exception {
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        Map<String, Object> claims = new HashMap(2);
        claims.put("username", "admin");
        claims.put("password", "010203");
        String jwtId = JWT_ID;
        long nowTime = System.currentTimeMillis();
        Date issuedAt = new Date(nowTime);
        SecretKey key = generalKey();
        JwtBuilder builder = Jwts.builder().setClaims(claims).setId(jwtId).setIssuedAt(issuedAt).setIssuer(issuer).setSubject(subject).signWith(signatureAlgorithm, key);
        long expTime = 604800000L;
        if (expTime >= 0L) {
            long exp = nowTime + expTime;
            builder.setExpiration(new Date(exp));
        }

        if (audience != null && !"".equals(audience)) {
            builder.setAudience(audience);
        } else {
            builder.setAudience("Tom");
        }

        return builder.compact();
    }

    public static Claims parseJWT(String jwt) throws Exception {
        SecretKey key = generalKey();
        Claims claims = (Claims)Jwts.parser().setSigningKey(key).parseClaimsJws(jwt).getBody();
        return claims;
    }
}
